//
//  XTXNetworking.m
//  xiaotianxin
//
//  Created by XYC on 2019/6/20.
//  Copyright © 2019 XYC. All rights reserved.
//

#import "XTXNetworking.h"

static XTXNetworking *tool = nil;


@implementation XTXNetworking

+ (void)networkingedituserdegerenxinxidewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:xiugaiyonghudegerenxinxidewangluoqingqiu parameters:parameters completionHandler:completionHandler];
}

+ (void)networkingShangchuangtupiandewangluoqingqiu:(NSData *) data completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou1:charutupiandewangluojiekou parameters:data completionHandler:completionHandler];
}


+ (void)networkingGetImV2Eveedewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:getImV2dewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)networkingGetOnlinPenyoudewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:getNewUserdewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)networkingUseSendMsgdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:insertMsgdewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)networkingGetUseMsgdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:listMessageListsdewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)networkingGetAllUseMsgdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:listMessagesdewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)networkingDelUseMsgdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:deleteMessageListdewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)networkingGetUseHuoyueListdewangluojiekou:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:listIndexDatadewangluojiekou parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingGetUseTuijianListdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:listRecommendDatadewangluojiekou parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingGetUseGuanzuListdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:zanshiguanzhulistInterface parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingPushInfoClickdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:jianchaxiangqingpushmsgdewangluojiekou parameters:parameters completionHandler:completionHandler];
}



+ (void)networkingDelUseXitongMsgdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:deleteSystemMessagedewangluojiekou parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingDelUseHudongMsgdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:deleteInteractionMessagedewangluojiekou parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingUpUseLoginHeadImagedewangluoqingqiu:(NSData *) data completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou1:denglutouxiangshangchuangdewangluojiekou parameters:data completionHandler:completionHandler];
}
 
+ (void)networkingUpUseImageViewdewangluoqingqiu:(NSData *) data completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou1:charutupiandewangluojiekou parameters:data completionHandler:completionHandler];
}

+ (void)networkingUpUseVoicedewangluoqingqiu:(NSData *) data completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou1:charushengyindewangluojiekou parameters:data completionHandler:completionHandler];
}

+ (void)networkingUpUseVoice2dewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:xiugaiyongyuyuyintiaodewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)networkinglistUserVoicesEveedewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:huoquyonghushengyinlistdewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)networkinglistVoicesEveedewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:shengyinliebiaodewangluojiekou parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingUpUseShipinViewdewangluoqingqiu:(NSData *) data completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou1:charushipingdewangluojiekou parameters:data completionHandler:completionHandler];
}

+ (void)networkingGetUseeStatidewangluojiekou:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:getUserStatistics parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingGuanzhuCancelGuanzhuUsedewangluojiekou:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:shifouguanzhudewangluojiekou parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingShifouGuanzhuUsedewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:chakanshifouguanzhucirendewangluojiekou parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingGetUseTonghuaTimedewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:callinfodewangluojiekou parameters:parameters completionHandler:completionHandler];
}






+ (void)networkinggetxiucanglistdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:listShowPlacedewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)GetUseXiuchangList:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:pengyouxiangcedewangluoJiekou parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingAddUsePlacedewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:xiuchangdewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)networkingdedaowodewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:xiuchnagliebiaodewangluojiekou parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingshanchushipingdeyaoqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:yichuxiuchangdewangluojiekou parameters:parameters completionHandler:completionHandler];
}



+ (void)networkingshanchuwodexiangcedewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:shanchutupiandewangluojiekou parameters:parameters completionHandler:completionHandler];
}



+ (void)networkingtianjiawdetupiandewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:tianjiayonghutouxiangdewangluojiekou parameters:parameters completionHandler:completionHandler];
}


+ (void)networkinghuoqudewxiangvcedewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:yonghutupiandewangluoqingqiu parameters:parameters completionHandler:completionHandler];
}


+ (void)networkinghuoquyaoqiulistdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:SubordinateListdewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)networkingtuichudengludewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:tuichuDengluJiekou parameters:parameters completionHandler:completionHandler];
}


+ (void)networkingGetUseInfowangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:getUserById parameters:parameters completionHandler:completionHandler];
}


+ (void)networkinglaheidewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:jiaruheimingdandewangluojiekou parameters:parameters completionHandler:completionHandler];
}

+ (void)networkingjubaodewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:yonghujiebaodewangluojiekou parameters:parameters completionHandler:completionHandler];
}
+(void)networkingshimingrenzhengdewangluoqingqiu:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self RequstDataJiekou:applyGoddessdewangluojiekou parameters:parameters completionHandler:completionHandler];
}
+(void)cancellationAccountNetworking:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler{
    [self RequstDataJiekou:closeUserAccount parameters:parameters completionHandler:completionHandler];
}
+ (void)RequstDataJiekou:(NSString *) urlStr parameters:(NSDictionary *) parameters completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self isnetworking];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?token=%@",Portaddress, urlStr, yonghuToken]];
//    //NSLog(@"URL == %@, parameters == %@", url, parameters);
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *data = [XTXGongTool AesJiam:parameters key:yonghuJiamiKey];
//    //NSLog(@"data == %@", data);
    request.HTTPMethod = @"post";
    request.HTTPBody = [data dataUsingEncoding:NSUTF8StringEncoding];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        if (data.length > 0) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:Nil];
            //NSLog(@"URL == %@, RequstDataJiekou == %@", url, dict);
            if ([dict[@"code"] integerValue] == 509) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [XTXGongView denglujiemiandezhanshiEventClick];
                });
            }
            else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (completionHandler) {
                        completionHandler(dict);
                    }
                });
            }
        }
        else {
            NSDictionary *dict = @{@"errorKey":@"无数据返回"};
            if (error) {
                dict = @{@"errorKey":error};
            }
            if (completionHandler) {
                completionHandler(dict);
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [XTXGongView showTipMsg:@"当前网络不稳定，请稍后再试！"];
            });
        }
        
    }];
    [task resume];
}

+ (void)RequstDataJiekou1:(NSString *) urlStr parameters:(NSData *) data completionHandler:(void (^)(NSDictionary * dict))completionHandler
{
    [self isnetworking];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?token=%@",Portaddress,urlStr, yonghuToken]];
    //NSLog(@"URL == %@, parameters == %@", url, data);
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    request.HTTPMethod = @"post";
    request.HTTPBody = data;
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error) {
            NSDictionary *dict = @{@"errorKey":error};
            if (completionHandler) {
                completionHandler(dict);
            }
        }else{
            if (data.length > 0) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:Nil];
                //NSLog(@"URL == %@, RequstDataJiekou == %@", url, dic);
                if (completionHandler) {
                    completionHandler(dic);
                }
            }
        }
        
        
    }];
    [task resume];
}


+ (void)isnetworking
{
    AFNetworkReachabilityManager *networkManager = [AFNetworkReachabilityManager sharedManager];
    [networkManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                [XTXGongView showTipMsg:@"暂无网络，请检查网络连接"];
                break;
            case AFNetworkReachabilityStatusNotReachable:
                [XTXGongView showTipMsg:@"暂无网络，请检查网络连接"];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                break;
            default:
                break;
        }
    }];
    [networkManager startMonitoring];
}

+ (id)share
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if(tool == nil)
        {
            tool = [[self alloc] init];
        }
    });
    return tool;
}

+ (id)allocWithZone:(NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (tool == nil)
        {
            tool = [super allocWithZone:zone];
        }
    });
    return tool;
}

// 如果有其他初始化操作，可在这里进行初始化
- (id)init
{
    self = [super init];
    if (self != nil)
    {
        // 其他初始化操作
    }
    return self;
}

// 防止外界拷贝造成多个实例，保证实例的唯一性。
- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

@end
